
import {Routes, Route, Navigate} from 'react-router-dom'
import workPathList, {MenuFormat} from './config'
import AsyncComponent from './AsyncComponent'

const _mapRouteFromMenuFormat = (pathItem: MenuFormat) => {
  if (pathItem?.children?.length ) {
    let routeList:Array<any> = pathItem.children.map(childItem => (_mapRouteFromMenuFormat(childItem)));
    routeList.unshift(<Route key={pathItem.path} path={pathItem.path} element={<Navigate to={'/work' + pathItem.children[0].path} />} />)
    return routeList;
  } else {
    let TempComp = AsyncComponent(() => import(`../pages/${pathItem.pageComponent}`))
    return (<Route path={pathItem.path} element={<TempComp />} key={pathItem.path}/>)
  }
}

function CombineRoute() {

  return (
    <Routes>
      <Route index element={<Navigate to="./playground" />} />
      {workPathList.map(_mapRouteFromMenuFormat)}
      <Route path="*" element={<h2>默认内容内容</h2>}/>
    </Routes>
  )
}

export default CombineRoute;

